﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: ImputeWithMedian.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using BOL.Linq.Descriptive;

namespace BOL.Linq.Transformation
{
    public static partial class Imputation
    {
        public static IEnumerable<decimal> ImputeWithMedian(this IEnumerable<decimal?> source)
        {
            var list = source.ToList();
            var median = list.Coalesce().Median();

            return list.ImputeWithConstant(median);
        }

        public static IEnumerable<double> ImputeWithMedian(this IEnumerable<double?> source)
        {
            var list = source.ToList();
            var median = list.Coalesce().Median();

            return list.ImputeWithConstant(median);
        }

        public static IEnumerable<float> ImputeWithMedian(this IEnumerable<float?> source)
        {
            var list = source.ToList();
            var median = list.Coalesce().Median();

            return list.ImputeWithConstant(median);
        }

        public static IEnumerable<int> ImputeWithMedian(this IEnumerable<int?> source)
        {
            var list = source.ToList();
            var median = list.Coalesce().Median();

            return list.ImputeWithConstant((int)median);
        }

        public static IEnumerable<long> ImputeWithMedian(this IEnumerable<long?> source)
        {
            var list = source.ToList();
            var median = list.Coalesce().Median();

            return list.ImputeWithConstant((long)median);
        }

        public static IEnumerable<decimal> ImputeWithMedian<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
        {
            return source.Select(selector).ImputeWithMedian();
        }

        public static IEnumerable<double> ImputeWithMedian<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector)
        {
            return source.Select(selector).ImputeWithMedian();
        }

        public static IEnumerable<float> ImputeWithMedian<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector)
        {
            return source.Select(selector).ImputeWithMedian();
        }

        public static IEnumerable<int> ImputeWithMedian<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector)
        {
            return source.Select(selector).ImputeWithMedian();
        }

        public static IEnumerable<long> ImputeWithMedian<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector)
        {
            return source.Select(selector).ImputeWithMedian();
        }
    }
}
